package com.pinda.service.impl;

import com.pinda.dao.AddressToCoordinate;
import com.pinda.dao.UUIDToDigits;
import com.pinda.dao.vo.ReturnLocationBean;
import com.pinda.oms.entity.PdOrder;
import com.pinda.oms.entity.PdOrderLocation;
import com.pinda.mapper.PdOrderLocationMapper;
import com.pinda.service.IPdOrderLocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * <p>
 * 订单位置信息  服务实现类
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@Service
public class PdOrderLocationServiceImpl extends ServiceImpl<PdOrderLocationMapper, PdOrderLocation> implements IPdOrderLocationService {

    @Resource
    private PdOrderLocationMapper pdOrderLocationMapper;

    @Override
    public boolean saveOrderLocation(PdOrder pdOrder) {
        PdOrderLocation pdOrderLocation = new PdOrderLocation();
        AddressToCoordinate addressToCoordinate = new AddressToCoordinate();

        ReturnLocationBean returnLocationBean1 = addressToCoordinate.getReturnLocationBean(pdOrder.getSenderAddress());
        ReturnLocationBean returnLocationBean2 = addressToCoordinate.getReturnLocationBean(pdOrder.getReceiverAddress());


        UUIDToDigits uuidToDigits = new UUIDToDigits();

        pdOrderLocation.setId("L"+uuidToDigits.uuidToDigits());
        pdOrderLocation.setOrderId(pdOrder.getId());


        if (returnLocationBean1.getStatus()==0&&returnLocationBean2.getStatus()==0){
            pdOrderLocation.setSendLocation(returnLocationBean1.getResult().getLocation().getLng()+","+returnLocationBean1.getResult().getLocation().getLat());
            pdOrderLocation.setReceiveLocation(returnLocationBean2.getResult().getLocation().getLng()+","+returnLocationBean2.getResult().getLocation().getLat());
        }else {
            pdOrderLocation.setStatus("0");
        }


        return this.pdOrderLocationMapper.insert(pdOrderLocation)!=0;
    }
}
